package com.gray.quartz.simple;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created on 2019/6/24.
 *
 * @author hy
 * @since 1.0
 */
@RestController
public class SimpleQuartzControllerTest {

    @Autowired
    SimpleQuartz simpleQuartz;

    @RequestMapping("/simple")
    public void simple() throws SchedulerException {
        Thread top = Thread.currentThread();
        System.out.println("topName:" + top.getName()+ "topId:" + top.getId());
        AtomicInteger value = new AtomicInteger(0);
        long startTime = new Date().getTime();

        Runnable runnable = () ->{
            long endTime = new Date().getTime();
            if(endTime - startTime <= 1000 + 500) {
                value.addAndGet(1);
            }
        };

        for(int i = 0;i < 100000 ;i ++){
            simpleQuartz.delayTimmer(1,runnable);
            simpleQuartz.delayTimmer(1,runnable);
            simpleQuartz.delayTimmer(1,runnable);
            simpleQuartz.delayTimmer(1,runnable);
        }

        try {
            Thread.sleep(5000);
            System.out.println(value.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
